package cn.edu.sdjzu.xg.kcsj.security;

import cn.edu.sdjzu.xg.kcsj.exception.BysjException;
import cn.edu.sdjzu.xg.kcsj.util.JdbcHelper;
import cn.edu.sdjzu.xg.kcsj.util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.TreeSet;

public class UserAssRoleService {
    private static UserAssRoleDao userAssRoleDao = UserAssRoleDao.getInstance();
    private static UserAssRoleService userAssRoleService = new UserAssRoleService();

    public static UserAssRoleService getInstance(){
        return userAssRoleService;
    }

    // 查找全部院系
    public List<UserAssRole> findAll(List conditionList, Pagination pagination) throws SQLException, BysjException {
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        List<UserAssRole> userAssRoles = new ArrayList<>();
        try {
            userAssRoles = userAssRoleDao.findAll(conditionList, pagination, connection);
            //提交事务
            connection.commit();
        } catch (SQLException e) {
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return userAssRoles;
    }

    // 按id查找院系
    public UserAssRole find(Integer id) throws SQLException,BysjException{
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        UserAssRole userAssRole = null;
        try {
            userAssRole = userAssRoleDao.find(id,connection);
            //提交事务
            connection.commit();
        }catch (SQLException e){
            //回滚
            connection.rollback();
        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return userAssRole;
    }

    public boolean update(UserAssRole userAssRole) throws SQLException,BysjException{
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Boolean updated =false;
        try {
            updated = userAssRoleDao.update(userAssRole,connection);
            //提交事务
            connection.commit();
        }catch (SQLException e){
            //回滚
            connection.rollback();
        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return updated;
    }

    public boolean add(UserAssRole userAssRole) throws SQLException,BysjException{
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Boolean added =false;
        try {
            //增加
            added = userAssRoleDao.add(userAssRole,connection);
            //提交事务
            connection.commit();
        }catch (SQLException e){
            //回滚
            connection.rollback();

        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return added ;
    }

    // 删除
    public boolean delete(Integer id) throws SQLException,BysjException {
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Boolean deleted = false;
        try {
            // 如果有子节点
            deleted = userAssRoleDao.delete(id, connection);
            //提交事务
            connection.commit();
        } catch (SQLException e) {
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return deleted;
    }

    public Collection<Role> findRolesByUserId(int id) throws SQLException{
        Collection<Role> roles = new TreeSet<>();
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        try {
            // 如果有子节点
            roles = userAssRoleDao.findRolesByUserId(id, connection);
            //提交事务
            connection.commit();
        } catch (SQLException e) {
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return roles;
    }
}
